package com.zhht.checkcop.demo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import com.zhht.checkcop.db.MysqlDb;
import com.zhht.checkcop.db.StaticDBMap;
import com.zhht.checkcop.parser.Parser88dusChapter;
import com.zhht.checkcop.util.Mail;

public class Update88dusChapter implements PageProcessor{
	private final static Logger log= Logger.getLogger(Update88dusChapter.class);
	
	private Site site = Site.me().setCycleRetryTimes(3).setSleepTime(1500).setTimeOut(5000);
	private List<String> novelListUrl;
	public Update88dusChapter(){}
	public Update88dusChapter(List<String> novelListUrl){
		this.novelListUrl=novelListUrl;
	}
	
	private Boolean bool=true;
	
	public void process(Page page) {
    	try{
    		if(bool){
    			bool=false;
    			page.addTargetRequests(novelListUrl);
    		}else{
    			String description=page.getHtml().xpath("//div[@class='intro']/text()").get();
		    	String imgUrl=page.getHtml().xpath("//div[@class='jieshao']/div[@class='lf']/img/@src").get();
		    	List<String> catalog=page.getHtml().xpath("//div[@class='mulu']/ul/li/a/@href").all();
		    	List<String> catalogName=page.getHtml().xpath("//div[@class='mulu']/ul/li/a/text()").all();
		    	String name = page.getHtml().xpath("//div[@class='rt']/h1/text()").get();
		    	String author_name = page.getHtml().xpath("//div[@class='rt']/div[@class='msg']/em/text(0)").get();
		    	author_name = author_name.replace("：", ":").split(":")[1].trim();
		    	
//		    		if(null != catalog && catalog.size()>0){
			    	page.putField("description", description);
			    	page.putField("imgUrl", imgUrl);
			    	page.putField("catalog", catalog);
			    	page.putField("catalogName", catalogName);
			    	page.putField("name", name);
			    	page.putField("author_name", author_name);
			    	page.putField("novelUrl", page.getRequest().getUrl());
			    	log.info("当前小说:"+name);
//		    	}
    		}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
    	SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
    	String synStateNum="0";
		for(int i=0;i<3;i++){
			SynchronizeNewNovel(args);

			try{
				MysqlDb mysqldb = new MysqlDb();
	    		mysqldb.connect();
	        	List<String> synState = mysqldb.queryListStr("select count(1) from novel_info where source = 1 and thisSyn = 0 and d = 0");
	        	if(synState!=null&&"0".equals(synState.get(0))){
	        		mysqldb.execute("update novel_info set thisSyn = 0 where source = 1 and thisSyn = 1 and d = 0");
	        		Mail.send("时间："+sdf.format(new Date())+"   本次同步书籍全部成功!");
	        		return;
	        	}
	        	synStateNum=synState.get(0);
	        	mysqldb.close();
			}catch (Exception e) {
				e.printStackTrace();
			}
        	
		}
		Mail.send("时间："+sdf.format(new Date())+"  验证 sql:select count(1) from novel_info where source = 1 and thisSyn = 0 and d = 0  失败同步书籍数量:"+synStateNum);
    }
	private static void SynchronizeNewNovel(String[] args) {
		try{
        	MysqlDb mysqldb = new MysqlDb();
        	mysqldb.connect();
        	List<String> strList1 = mysqldb.queryListStr("select source_url from novel_info where source = 1 and classify_id = 1 and thisSyn = 0 and d = 0");
        	StaticDBMap.existNovelNameMap = mysqldb.query("select id,checkSynchronizeType,chapter_count,`name`,author_name,source_url from novel_info where source = 1 and thisSyn = 0 and d = 0",new ArrayList(),"source_url");
        	StaticDBMap.classfiyTableName = mysqldb.query("select classify_id,CONCAT(table_name,classify_num) tableName from classify_direction where d = 0",new ArrayList<Object>(),"classify_id");
        	
        	int thread = args!=null&&args.length>0?Integer.parseInt(args[0]):10;
        	List<String> strList2 = mysqldb.queryListStr("select source_url from novel_info where source = 1 and classify_id = 2 and thisSyn = 0 and d = 0");
        	List<String> strList3 = mysqldb.queryListStr("select source_url from novel_info where source = 1 and classify_id = 3 and thisSyn = 0 and d = 0");
        	List<String> strList4 = mysqldb.queryListStr("select source_url from novel_info where source = 1 and classify_id = 4 and thisSyn = 0 and d = 0");
        	List<String> strList5 = mysqldb.queryListStr("select source_url from novel_info where source = 1 and classify_id = 5 and thisSyn = 0 and d = 0");
        	List<String> strList6 = mysqldb.queryListStr("select source_url from novel_info where source = 1 and classify_id = 6 and thisSyn = 0 and d = 0");
        	List<String> strList7 = mysqldb.queryListStr("select source_url from novel_info where source = 1 and classify_id = 7 and thisSyn = 0 and d = 0");
        	List<String> strList8 = mysqldb.queryListStr("select source_url from novel_info where source = 1 and classify_id = 8 and thisSyn = 0 and d = 0");
        	List<String> strList9 = mysqldb.queryListStr("select source_url from novel_info where source = 1 and classify_id = 9 and thisSyn = 0 and d = 0");
        	mysqldb.close();
        	
        	Spider.create(new Update88dusChapter(strList1)).addUrl("https://www.88dus.com/").addPipeline(new Parser88dusChapter("1")).thread(thread).run();
        	log.info("同步新小说首次菜单玄幻魔法已完成");
        	Spider.create(new Update88dusChapter(strList2)).addUrl("https://www.88dus.com/").addPipeline(new Parser88dusChapter("2")).thread(thread).run();
        	log.info("同步新小说首次菜单武侠修真已完成");
        	Spider.create(new Update88dusChapter(strList3)).addUrl("https://www.88dus.com/").addPipeline(new Parser88dusChapter("3")).thread(thread).run();
        	log.info("同步新小说首次菜单都市言情已完成");
        	Spider.create(new Update88dusChapter(strList4)).addUrl("https://www.88dus.com/").addPipeline(new Parser88dusChapter("4")).thread(thread).run();
        	log.info("同步新小说首次菜单历史穿越已完成");
        	Spider.create(new Update88dusChapter(strList5)).addUrl("https://www.88dus.com/").addPipeline(new Parser88dusChapter("5")).thread(thread).run();
        	log.info("同步新小说首次菜单恐怖悬疑已完成");
        	Spider.create(new Update88dusChapter(strList6)).addUrl("https://www.88dus.com/").addPipeline(new Parser88dusChapter("6")).thread(thread).run();
        	log.info("同步新小说首次菜单游戏竞技已完成");
        	Spider.create(new Update88dusChapter(strList7)).addUrl("https://www.88dus.com/").addPipeline(new Parser88dusChapter("7")).thread(thread).run();
        	log.info("同步新小说首次菜单军事科幻已完成");
        	Spider.create(new Update88dusChapter(strList8)).addUrl("https://www.88dus.com/").addPipeline(new Parser88dusChapter("8")).thread(thread).run();
        	log.info("同步新小说首次菜单综合类型已完成");
        	Spider.create(new Update88dusChapter(strList9)).addUrl("https://www.88dus.com/").addPipeline(new Parser88dusChapter("9")).thread(thread).run();
        	log.info("同步新小说首次菜单女生频道已完成");
        	
    	}catch (Exception e) {
			e.printStackTrace();
		}
	}
}
